{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wind Turbine combined with Heat Pump and Water Tank\n",
    "\n",
    "In this example the heat demand is supplied by a wind turbine in combination with a heat pump and a water tank that stores hot water with a standing loss."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pypsa\n",
    "import pandas as pd\n",
    "from pyomo.environ import Constraint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network = pypsa.Network()\n",
    "network.set_snapshots(pd.date_range(\"2016-01-01 00:00\",\"2016-01-01 03:00\", freq=\"H\"))\n",
    "\n",
    "network.add(\"Bus\", \"0\", carrier=\"AC\")\n",
    "network.add(\"Bus\", \"0 heat\", carrier=\"heat\")\n",
    "\n",
    "network.add(\"Carrier\", \"wind\")\n",
    "network.add(\"Carrier\", \"heat\")\n",
    "\n",
    "network.add(\"Generator\",\n",
    "            \"wind turbine\",\n",
    "            bus=\"0\",\n",
    "            carrier=\"wind\",\n",
    "            p_nom_extendable=True,\n",
    "            p_max_pu=[0.,0.2,0.7,0.4],\n",
    "            capital_cost=500)\n",
    "\n",
    "network.add(\"Load\",\n",
    "            \"heat demand\",\n",
    "            bus=\"0 heat\",\n",
    "            p_set=20.)\n",
    "\n",
    "#NB: Heat pump has changing efficiency (properly the Coefficient of Performance, COP)\n",
    "#due to changing ambient temperature\n",
    "network.add(\"Link\",\n",
    "            \"heat pump\",\n",
    "            bus0=\"0\",\n",
    "            bus1=\"0 heat\",\n",
    "            efficiency=[2.5,3.,3.2,3.],\n",
    "            capital_cost=1000,\n",
    "            p_nom_extendable=True)\n",
    "    \n",
    "network.add(\"Store\",\n",
    "            \"water tank\",\n",
    "            bus=\"0 heat\",\n",
    "            e_cyclic=True,\n",
    "            e_nom_extendable=True,\n",
    "            standing_loss=0.01)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.lopf(network.snapshots)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame({attr: network.stores_t[attr][\"water tank\"] for attr in [\"p\",\"e\"]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame({attr: network.links_t[attr][\"heat pump\"] for attr in [\"p0\",\"p1\"]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.stores.loc[[\"water tank\"]].T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.generators.loc[[\"wind turbine\"]].T"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
